package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ALbums extends AppCompatActivity implements View.OnClickListener {

    private ImageView imageView;
    private TextView txt;
    private Button btn_pre;
    private Button btn_next;
    private int[] images;
    private String[] names;
    private int index;
    private int btn_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_albums);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        /**
         * 初始化

         */
        initView();
        initData();
        btn_pre.setOnClickListener(this);
        btn_next.setOnClickListener(this);
    }
    private  void initView(){
        imageView = findViewById(R.id.image);
        txt = findViewById(R.id.text);
        btn_pre = findViewById(R.id.button_pre);
        btn_next = findViewById(R.id.button_next);
    }
    private void initData(){
        images = new int[]{R.drawable.b,R.drawable.c,R.drawable.c,R.drawable.d};
        names = new String[]{"第一张","第二张","第三张","第四张"};
        imageView.setImageResource(images[0]);
        txt.setText(names[0]);
        index = 0;
    }

    @Override
    public void onClick(View v) {
        btn_id = v.getId();
        if (btn_id == R.id.button_pre) {
            if (index == 0) {
                Toast.makeText(this, "已经是第一张！", Toast.LENGTH_SHORT).show();
                return;
            } else {
                index--;
            }
        } else if (btn_id == R.id.button_next) {
            if (index == images.length - 1) {
                Toast.makeText(this, "已经是最后一张！", Toast.LENGTH_SHORT).show();
                return;
            } else {
                index++;
            }
        }
        upDateView();
    }




    private void upDateView() {
        imageView.setImageResource(images[index]);
        txt.setText(names[index]);

     

        // 如果需要循环则注释掉禁用逻辑，保留以下代码：
        // btn_pre.setEnabled(true);
        // btn_next.setEnabled(true);
    }
}